#include "user_extern.h"

static uint8_t run(void);
static void win_create(void);
static void win_destory(void);
static void update_screen(void);
static void display(void);
static uint8_t button_select(void *arg);
static uint8_t button_enter(void *arg);
static uint8_t button_exit(void *arg);
static uint8_t get_id(void);
static void menu_select(uint8_t menu);

static Windows window = {
    .id = WIN_UI_DIGIMON,
    .fa_id = WIN_UI_DIGIMON,
    .son_id = {0},
    .name = "digimon",
    .win_status = WINDOWS_STATUS_INIT,
    .run = run,
    .update_screen = update_screen,
    .Dselect = button_select,
    .enter = button_enter,
    .exit = button_exit,
};

static uint8_t menuidx = 0;

static void win_display(void)
{
    uint8_t tiki = mtime.g_timestamp % 2;

    if (status_cek_flag(digimon.digi_status, STA_SHIRT))
    {
        digimon.show_shirt(tiki);
    }
    if (status_cek_flag(digimon.digi_status, STA_ILL))
    {
        digimon.show_ill();
    }
    digimon.display(tiki);
    digimon.show_sys_menu(menuidx);
}

static void update_screen(void)
{
    oled.refresh_all(1);
}

static uint8_t button_select(void *arg)
{
    menuidx++;
    if (menuidx > 10)
    {
        menuidx = 0;
    }
    return 0;
}

static uint8_t button_enter(void *arg)
{
    if (menuidx == 0)
    {
        Dsystem.windows_cur_id = WIN_UI_DESKTOP;
    }
    else
    {
        Dsystem.windows_cur_id = WIN_UI_OTHERMO;
        Dsystem.windows_sub_id = WIN_SDIGI_MENU1_INFO + menuidx -1;
    }
    window.win_status = WINDOWS_STATUS_INIT;
    menuidx = 0;
    return 0;
}

static uint8_t button_exit(void *arg)
{
    menuidx = 0;
    return 0;
}

/* **************************************************************************************** */
static uint8_t run(void)
{

    switch (window.win_status)
    {
    case WINDOWS_STATUS_NONE:
        return 0;
        break;
    case WINDOWS_STATUS_INIT:
    case WINDOWS_STATUS_DIS_ONLY:
        win_display();
        window.win_status = WINDOWS_STATUS_DIS_ONLY;
        break;
    }
    return window.win_status;
}

static uint8_t get_id(void)
{
    return window.id;
}

void win03_digimon_init(void)
{
    windows[get_id()] = window;
}
